import SwiftUI

/*
 * 菜单示例
 */
class MenuExample: FCUIView{
    var gMenu = FCMenu()
    
    /*
    * 触摸开始方法
    * view:视图
    * firstTouch:是否第一次触摸
    * firstPoint:第一次触摸的坐标
     * secondTouch:是否第二次触摸
    * secondPoint:第二次触摸的坐标
    */
    func onTouchBegin(view:FCView, firstTouch:Bool, firstPoint:FCPoint, secondTouch:Bool, secondPoint:FCPoint) {
        onTouchBeginDefault(view:view, firstTouch:firstTouch,  firstPoint:firstPoint, secondTouch:secondTouch, secondPoint:secondPoint)
        if(!gMenu.visible){
            gMenu.location = FCPoint(x:view.getPaint().touchDownPoint.x, y:view.getPaint().touchDownPoint.y)
            gMenu.visible = true
            adjustMenu(menu:gMenu)
            invalidate(paint:gMenu.getPaint())
        }
    }
    
    /*
     * 初始化布局
     */
    override func onLoad(){
        let isInit = super.m_isInit;
        super.onLoad();
        if !isInit{
            m_paint.defaultUIStyle = "light"
            let width = Double(self.frame.size.width), height = Double(self.frame.size.height)
            renderFaceCat(name:"menuexample", paint: m_paint, size: FCSize(cx:width, cy:height))
            m_paint.addTouchBeginEvent(callBack: onTouchBegin)
            gMenu.size = FCSize(cx:100, cy:300)
            addView(view:gMenu, paint:m_paint)
            for _ in 0...9{
                let menuItem = FCMenuItem()
                menuItem.text = "菜单项"
                addMenuItem(item:menuItem, menu:gMenu)
                for _ in 0...9{
                    let subMenuItem = FCMenuItem()
                    subMenuItem.text = "子菜单项"
                    addMenuItemToParent(item:subMenuItem, parentItem: menuItem)
                    for _ in 0...9{
                        let sunMenuItem = FCMenuItem()
                        sunMenuItem.text = "孙菜单项"
                        addMenuItemToParent(item:sunMenuItem, parentItem:subMenuItem)
                    }
                }
            }
        }
    }
}
